﻿@page "/admin/tools"
@attribute [Authorize(Roles = "Admin")]

@inject IHttpService _httpService


@inject NavigationManager NavigationManager
@inject IPopupService PopupService


<CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip Title="工具"/>

<div class="">
    <div class="row">
        <div class="col-12 col-sm-12 col-md-6 col-lg-6 col-xl-4 pb-3">
            <AdminToolTip Title="刷新搜索缓存" OnToolClick="OnRefreshSearchData"
                          BriefIntroduction="将会删除ES数据并重新设置，建议重启ES后执行">
            </AdminToolTip>
        </div>
        <div class="col-12 col-sm-12 col-md-6 col-lg-6 col-xl-4 pb-3">
            <AdminToolTip Title="执行临时脚本" OnToolClick="OnGetSteamImage"
                          BriefIntroduction="将会执行预设置好的临时脚本">
            </AdminToolTip>
        </div>
    </div>

</div>
@code {

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    public async Task OnRefreshSearchData()
    {
        try
        {
            await PopupService.ToastSuccessAsync("正在刷新搜索缓存", "任务执行需要一定时间，请稍安勿躁哦");
            var obj = await _httpService.GetAsync<Result>("api/admin/RefreshSearchData");
            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync("刷新搜索缓存失败", obj.Error);
            }
            else
            {
                await PopupService.ToastSuccessAsync("成功刷新搜索缓存", "成功刷新搜索缓存");
            }
        }
        catch(Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "刷新搜索缓存失败");
        }
    }
    public async Task OnGetSteamImage()
    {
        try
        {
            await PopupService.ToastSuccessAsync("正在执行临时脚本", "任务执行需要一定时间，请稍安勿躁哦");
            var obj = await _httpService.GetAsync<Result>("api/admin/TempFunction");
            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync("执行临时脚本失败", obj.Error);
            }
            else
            {
                await PopupService.ToastSuccessAsync("成功执行临时脚本", "成功执行临时脚本");
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "执行临时脚本失败");
        }
    }
}

